articles

Home / DeveloperSection / Articles / Factory vs Abstract Factory Design Patterns in Java

Factory vs Abstract Factory Design Patterns in Java

Factory vs Abstract Factory Design Patterns in Java

Ashutosh Kumar Verma 160 22-Jul-2024

Factories and Abstract Factories are two of the most important design structures. Let’s take a closer look at each one,

 

Factory Pattern

The Factory Pattern is a creation model that provides an interface for creating objects in a superclass, but allows subclasses to change the type of object to be created This encourages loose coupling by detaching the object creation process from client code.

 

Key Components

 

Factory Interface/Class- Defines the method. This can be an interface or an abstract class.

 

Concrete Factories- Use a factory interface to create a specific product.

 

Product- Manufacturing facilities. These can be concrete classes or interfaces.

 

Example-

// Product interface
interface Shape {
   void draw();
}
// Concrete products
class Circle implements Shape {
   @Override
   public void draw() {
       System.out.println("Inside Circle::draw() method.");
   }
}
class Rectangle implements Shape {
   @Override
   public void draw() {
       System.out.println("Inside Rectangle::draw() method.");
   }
}
// Factory interface
interface ShapeFactory {
   Shape createShape();
}
// Concrete factories
class CircleFactory implements ShapeFactory {
   @Override
   public Shape createShape() {
       return new Circle();
   }
}
class RectangleFactory implements ShapeFactory {
   @Override
   public Shape createShape() {
       return new Rectangle();
   }
}
// Client code
public class FactoryPatternExample {
   public static void main(String[] args) {
       ShapeFactory circleFactory = new CircleFactory();
       Shape circle = circleFactory.createShape();
       circle.draw();
       
       ShapeFactory rectangleFactory = new RectangleFactory();
       Shape rectangle = rectangleFactory.createShape();
       rectangle.draw();
   }
}

 

Abstract Factory Pattern


The Abstract Factory Pattern provides an interface for creating families of related or dependent objects by specifying their concrete classes. Used when there are multiple components to be manufactured and you want to ensure compatibility of components.

 

Key Components


Abstract Factory- Exposes abstract processes.

 

Concrete Factories- Implement an abstract factory interface for the production of concrete objects.

 

Abstract Products- Declares interfaces for related products.

 

Concrete products- Use an abstract product interface to define specific objects.

 

Example

// Abstract product interfaces
interface Shape {
   void draw();
}
interface Color {
   void fill();
}
// Concrete product classes
class Circle implements Shape {
   @Override
   public void draw() {
       System.out.println("Inside Circle::draw() method.");
   }
}
class Red implements Color {
   @Override
   public void fill() {
       System.out.println("Inside Red::fill() method.");
   }
}
// Abstract factory interface
interface AbstractFactory {
   Shape createShape();
   Color createColor();
}
// Concrete factory implementations
class ShapeFactory implements AbstractFactory {
   @Override
   public Shape createShape() {
       return new Circle();
   }
   
   @Override
   public Color createColor() {
       // Not used in this example
       return null;
   }
}
class ColorFactory implements AbstractFactory {
   @Override
   public Shape createShape() {
       // Not used in this example
       return null;
   }
   
   @Override
   public Color createColor() {
       return new Red();
   }
}
// Client code
public class AbstractFactoryPatternExample {
   public static void main(String[] args) {
       AbstractFactory shapeFactory = new ShapeFactory();
       Shape circle = shapeFactory.createShape();
       circle.draw();
       
       AbstractFactory colorFactory = new ColorFactory();
       Color red = colorFactory.createColor();
       red.fill();
   }
}

 

Key Differences

 

Intent


Factory Pattern- Creates objects based on a generic interface, so that client code can manipulate these objects through the interface.

 

Abstract Factory Model- Provides an interface for creating families of related or dependent objects, ensuring product consistency.

 

Uses
 

Factory Pattern- Used when responsibility for manufacturing has to be transferred to another factory, especially useful when manufacturing is complex or when there are multiple production processes.

 

Abstract Factory Pattern- It used when there are multiple families of related objects or when the system needs flexibility in how it creates, organizes and represents its objects.


Both models simplify the creation process but satisfy conditions. Factory patterns are about creating individual objects, while abstract factory patterns are a family of related objects. Each model supports different levels of abstraction and flexibility in object creation in Java applications.

 

Also, Read: Explain the SOLID principle in Java programming


Updated 22-Jul-2024
Hi! This is Ashutosh Kumar Verma. I am a software developer at MindStick Software Pvt Ltd since 2021. I have added some new and interesting features to the MindStick website like a story section, audio section, and merge profile feature on MindStick subdomains, etc. I love coding and I have good knowledge of SQL Database.

Leave Comment

Comments

Liked By